<?php
class pagingClass {

    function limit_page($row_perpage)
    {
        if(isset($_REQUEST['page'])) {
            $start_page = ceil(($_REQUEST['page']-1)*$row_perpage);
        }else {
            $start_page = 0;
        }
        return $start_page;
    }

    function break_pagenum($row_count,$row_perpage,$url,$Search=null)
    {

        if($Search!=null) {
            $Search = '/search';
        }

        $htmlpagenum="";
        $paging_count=ceil($row_count/$row_perpage);
        $count=0;

        if($paging_count>1) {
            ///// First Page /////
            $htmlpagenum.='<table cellspacing="2" cellpadding="0" border="0"><tr>';
            if(isset($_REQUEST['page'])) {
                if($_REQUEST['page']==1) {
                    $htmlpagenum.='<td style="width: 40px;" onclick="location.href=\''.$url.'1'.$Search.'\'" class="paging-active">First</td>';
                }else {
                    $htmlpagenum.='<td style="width: 40px;" onclick="location.href=\''.$url.'1'.$Search.'\'" class="paging-normal">First</td>';
                }
            }else {
                $htmlpagenum.='<td style="width: 40px;" class="paging-active">First</td>';
            }

            ##### Limit Page Count 1,2,4 size
            $rel_paging_count = $paging_count;
            $start_page     = null;
            $end_page       = null;
            $limit_count    = 30;
            $current_page   = @$_REQUEST['page'];

            if($current_page > 1) {
                $start_page     = $current_page;
            }
            $end_page       = $start_page + $limit_count;

            if($current_page % $limit_count == 0) {
                $start_page     = $current_page;
                $_SESSION['sp'] = $start_page;              ### limit start page into session
                $end_page       = $start_page + $limit_count;
                $_SESSION['ep'] = $end_page;                ### limit end page into session

            }else {
                if($start_page > $limit_count) {
                    $start_page = $_SESSION['sp'];
                    $end_page   = $_SESSION['ep'];
                }else {
                    $start_page = 0;
                    $end_page   = $limit_count;
                }
            }

            $i = $start_page;
            if($end_page < $paging_count) {
                $paging_count   = $end_page;
            }else {
                if($start_page>$limit_count) {
                    $i = $start_page - $limit_count;
                }
            }

            ### go to previous 10 pages
            if($start_page>=$limit_count) {
                $pre = $current_page - $limit_count;
                if($pre <= 0) {
                    $pre = 1;
                }
                $htmlpagenum.='<td onclick="location.href=\''.$url.$pre.$Search.'\'" class="paging-normal">...</td>';
            }

            ##### Display Page Count 1,2,4
            while($i<$paging_count) {
                $i++;
                if(isset($_REQUEST['page'])) {
                    if($_REQUEST['page']==$i) {
                        $htmlpagenum.='<td class="paging-active">'.$i.'</td>';
                    }else {
                        $htmlpagenum.='<td onclick="location.href=\''.$url.$i.$Search.'\'" class="paging-normal" >'.$i.'</td>';
                    }
                }else {
                    if($i==1) {
                        $htmlpagenum.='<td class="paging-active">'.$i.'</td>';
                    }else {
                        $htmlpagenum.='<td onclick="location.href=\''.$url.$i.$Search.'\'" class="paging-normal">'.$i.'</td>';
                    }
                }
            }

            ### go to next 10 pages
            if($rel_paging_count>$paging_count) {
                $htmlpagenum.='<td onclick="location.href=\''.$url.$paging_count.$Search.'\'" class="paging-normal">...</td>';
            }

            ///// Last Page /////
            if(isset($_REQUEST['page'])) {
                if($_REQUEST['page']==$paging_count) {
                    $htmlpagenum.='<td style="width: 40px;" class="paging-active">Last</td>';
                }else {
                    $htmlpagenum.='<td style="width: 40px;" onclick="location.href=\''.$url.$paging_count.$Search.'\'" class="paging-normal">Last</td>';
                }
            }else {
                $htmlpagenum.='<td style="width: 40px;" onclick="location.href=\''.$url.$paging_count.$Search.'\'" class="paging-normal">Last</td>';
            }
        }
        $htmlpagenum.='</tr></table>';
        return $htmlpagenum;
    }
}

//******** HOW TO USE ********\\
/*$row_perpage=50;
$start_page=$pagingClass->limit_page($row_perpage);

$query = "SELECT * FROM table WHERE condition LIMIT $start_page, $row_perpage";

$total_rows = '';//count data from tabalse
if($total_rows>$row_perpage){
    $pagingClass->break_pagenum($total_rows,$row_perpage,'ecareer_list.php?cid='.$_REQUEST['cid'].'&page=');
}*/

?>